package com.example.sxproject.mapper;

import com.example.sxproject.pojo.FPSearch;
import com.example.sxproject.pojo.FinancialProduct;
import com.example.sxproject.pojo.FinancialUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FinancialMapper {

    //查询select：查询全部
    @Select("select * from financial_product")
    List<FinancialProduct> SelectAll();

    //查询：根据id查找
    @Select("select * from financial_user WHERE belongUser = #{userId}")
    List<FinancialUser> findByUserId(Integer userId);

    //查询select：根据id查找
    @Select("select * from financial_product WHERE id = #{productId}")
    FinancialProduct findByProductId(Integer productId);

    //查询select：根据用户名查找 产品-用户表
    @Select("SELECT p.* FROM financial_user u JOIN financial_product p ON u.belongProduct = p.id WHERE u.belongUser = #{userId}")
    List<FinancialProduct> SelectByUserId(Integer userId);

    //查询select：多条件查询操作
    @Select({
            "<script>",
            "SELECT *",
            "FROM financial_product",
            "<where>",
            "<if test='productName != null'>",
            "productName LIKE CONCAT('%', #{productName}, '%')",
            "</if>",
            "<if test='costStatus != null'>",
            "AND costStatus = #{costStatus}",
            "</if>",
            "<if test='riskType != null'>",
            "AND riskType = #{riskType}",
            "</if>",
            "<if test='ocType != null'>",
            "AND ocType = #{ocType}",
            "</if>",
            "<if test='lowTime != null'>",
            "AND timeContinue &gt;= #{lowTime}", // 处理 lowTime 的下限条件
            "</if>",
            "<if test='highTime != null'>",
            "AND timeContinue &lt;= #{highTime}", // 处理 highTime 的上限条件
            "</if>",
            "</where>",
            "</script>"
    })
    List<FinancialProduct> SelectFPSearch(FPSearch fpSearch);

    //添加insert：添加一个产品
    @Insert("insert into financial_product" +
            "(productName, buyBeginDate, buyEndDate, beginDate, endDate, timeContinue, incomeType, " +
            "beginMoney, costStatus, currencyType, ocType, riskType, institution, " +
            "lowRate, highRate, productStatus)" +
            " values (#{productName}, #{buyBeginDate}, #{buyEndDate}, #{beginDate}, " +
            "#{endDate}, #{timeContinue}, #{incomeType}, #{beginMoney}, #{costStatus}, #{currencyType}, " +
            "#{ocType}, #{riskType}, #{institution}, #{lowRate}, #{highRate}, " +
            "#{productStatus})"
    )
    void addFinancialProduct(FinancialProduct financialProduct);

    //添加insert：添加一个产品
    @Insert("insert into financial_user" +
            "(id, belongUser, belongProduct, balance, productName, productStatus)" +
            " values (#{id}, #{belongUser}, #{belongProduct}, #{balance}, #{productName}, #{productStatus})"
    )
    void addUserAndProduct(FinancialUser financialUser);

    //更新update：更新记录
    @Update("update financial_product set productName = #{productName}, " +
            "buyBeginDate = #{buyBeginDate}, buyEndDate = #{buyEndDate}, " +
            "beginDate = #{beginDate}, endDate = #{endDate}, timeContinue = #{timeContinue}, " +
            "incomeType = #{incomeType}, beginMoney = #{beginMoney}, " +
            "costStatus = #{costStatus}, currencyType = #{currencyType}, " +
            "ocType = #{ocType}, riskType = #{riskType}, " +
            "institution = #{institution}, lowRate = #{lowRate}, " +
            "highRate = #{highRate}, productStatus = #{productStatus} " +
            "where id = #{id}")
    void update(FinancialProduct financialProduct);

    //更新update：更新用户-产品
    @Update("update financial_user set belongUser = #{belongUser}, belongProduct = #{belongProduct}, " +
            "balance = #{balance}, productStatus = #{productStatus} " +
            "where belongProduct = #{belongProduct} and belongUser = #{belongUser}")
    void updateUP(FinancialUser financialUser);

    //更新update：实现假删除
    @Update("update financial_product set productStatus = #{productStatus} " +
            "where id = #{id}")
    void delete(FinancialProduct financialProduct);

    //管理员用户所有理财产品查询
    @Select({
            "<script>",
            "SELECT *",
            "FROM financial_user",
            "<where>",
            "<if test='belongUser!= null'>",
            "belongUser=#{belongUser}",
            "</if>",
            "<if test='belongProduct!= null'>",
            "AND belongProduct = #{belongProduct}",
            "</if>",
            "</where>",
            "</script>",
    })
    List<FinancialUser> selectFu(FinancialUser financialUser);
}
